Skip to content

deref_patterns: support string and byte string literals in explicit deref!("...") patterns #140028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Apr 19, 2025

When deref_patterns is enabled, this allows string literal patterns to be used where str is expected and byte string literal patterns to be used where [u8] or [u8; N] is expected. This lets them be used in explicit deref!("...") patterns to match on String, Box<str>, Vec<u8>, Box<[u8;N]>, etc. (as well as to match on slices and arrays obtained through other means). Implementation-wise, this follows up on #138992: similar to how byte string literals matching on &[u8] is implemented, this changes the type of the patterns as determined by HIR typeck, which informs const-to-pat on how to translate them to THIR (though strings needed a bit of extra work since we need references to call <str as PartialEq>::eq in the MIR lowering for string equality tests).

This PR does not add support for implicit deref pattern syntax (e.g. "..." matching on String, as string_deref_patterns allows). I have that implemented locally, but I'm saving it for a follow-up PR1.

This also does not add support for using named or associated constants of type &str where str is expected (nor likewise with named byte string constants). It'd be possible to add that if there's an appetite for it, but I figure it's simplest to start with literals.

This is gated by the deref_patterns feature since it's motivated by deref patterns. That said, its impact reaches outside of deref patterns; it may warrant a separate experiment and feature gate, particularly factoring in the follow-up1. Even without deref patterns, I think there's probably motivation for these changes.

The update to the unstable book added by this will conflict with #140022, so they shouldn't be merged at the same time.

Tracking issue for deref patterns: #87121

r? @oli-obk
cc @Nadrieril

Footnotes

  1. The piece missing from this PR to support implicit deref pattern syntax is to allow string literal patterns to implicitly dereference their scrutinees before matching (see resolve how to handle constants and default binding modes #44849). As a consequence, it also makes examples like the one in that issue work (though it's still gated by deref_patterns). I can provide more information on how I've implemented it or open a draft if it'd help in reviewing this PR. 2

dianne added 3 commits April 18, 2025 22:08
Specifically, this allows string literal patterns to be used where a
`str` is expected when `deref_patterns` is enabled.
Specifically, this allows byte string literal patterns to be used where
a `[u8]` or `[u8;N]` is expected when `deref_patterns` is enabled.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 19, 2025

Some changes occurred in match lowering

cc @Nadrieril

Some changes occurred in match checking

cc @Nadrieril

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants